package com.zhenyi.jiahua.index.admin;

import java.util.Date;

import org.joda.time.DateTime;

import com.jfinal.kit.Kv;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.SqlPara;
import com.jiahua.common.model.JhRecord;
import com.jiahua.common.model.JhStaff;
import com.jiahua.common.model.JhStudent;
import com.utils.DateUtils;
import com.zhenyi.jiahua.core.utils.MessageUtils;
import com.zhenyi.jiahua.index.JHController;

import cn.hutool.core.date.DateUtil;

/**
 * 学生记录
 * 
 * @author cen
 *
 */
public class StudentRecordController extends JHController {

	@Override
	public void index() {
		Long studentId = getParaToLong("studentId");
		String startDate = getPara("startDate",
				DateUtil.format(new DateTime().minusDays(1).toDate(), DateUtils.YYYYMMDD_HHMMSS));
		String endDate = getPara("endDate", DateUtil.format(new DateTime().toDate(), DateUtils.YYYYMMDD_HHMMSS));
		Long staffId = getParaToLong("staffId");

		if (studentId == null) {
			renderFailure(500, "student id is null");
			return;
		}
		JhStudent student = JhStudent.dao.findById(studentId);

		Kv kv = Kv.create();
		kv.set("studentId", studentId);
		if (StrKit.notBlank(startDate) && StrKit.notBlank(endDate)) {
			kv.set("startTime", startDate);
			kv.set("endTime", endDate);
		}
		if (staffId != null) {
			kv.set("staffId", staffId);
		}
		SqlPara sqlPara = Db.use("jiahua").getSqlPara("JHStudentRecord", kv);
		Page<Record> page = Db.use("jiahua").paginate(getPage(), PAGE_SIZE, sqlPara);
		setRainbow(page);
		setItems(jsonObject(page.getList()));
		setInfoItem(student);
		setAttr("startDate", startDate);
		setAttr("endDate", endDate);
		render("/jh/admin/student/record.html");
	}

	public void info() {
		Long recordId = getParaToLong("recordId");
		Long studentId = getParaToLong("studentId");
		if (recordId != null) {
			JhRecord info = JhRecord.dao.findById(recordId);
			if (info == null) {
				renderFailure(404, "record not found");
				return;
			}
			JhStudent student = JhStudent.dao.findById(info.getStudentId());
			setAttr("student", student);
			setInfoItem(info);
		} else if (studentId != null) {
			setAttr("student", JhStudent.dao.findById(studentId));
		} else {
			renderFailure();
			return;
		}
		render("/jh/admin/student/recordInfo.html");
	}

	public void submit() {
		Long recordId = getParaToLong("recordId");
		String content = getParaRelaceN("content");
		Long studentId = getParaToLong("studentId");
		Date notificationTime = getTimeDate("notificationTime");

		if (studentId == null) {
			renderFailure(500, "student id is null");
			return;
		}

		boolean isNew = recordId == null;
		JhRecord info = isNew ? new JhRecord() : JhRecord.dao.findById(recordId);
		if (!isNew && info == null) {
			renderFailure(404, "record not found");
			return;
		}
		info.setDate(new Date());
		info.setContent(content);
		info.setNotificationTime(notificationTime);

		if (isNew) {
			info.setStudentId(studentId);
			info.setStaffId(getStaff().getId());
			info.setAddTime();
		}
		boolean flag = isNew ? info.save() : info.update();
		if (flag) {
			renderSuccess();
		} else {
			renderFailure();
		}
		JhStaff staff = getStaff();
		Long staffId = staff.getId();
		JhStudent student = JhStudent.dao.findById(studentId);
		String sendTitle = "学员" + student.getName() + "有新的学生记录,请查看";
		String sendContent = content;
		MessageUtils.send(student.getCcId(), staffId, sendTitle, sendContent);
		MessageUtils.send(student.getTaId(), staffId, sendTitle, sendContent);
	}

	public void delete() {
		Long recordId = getParaToLong("recordId");
		if (recordId == null) {
			renderFailure(500, "id is null");
			return;
		}
		JhRecord info = JhRecord.dao.findById(recordId);
		if (info == null) {
			renderFailure(404, "record not found");
			return;
		}
		renderFlag(info.delete());
	}

}
